#!/usr/bin/env sh
set -e
CURRENT_DIR=$(pwd)
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="${SCRIPT_DIR}/.."
TMP_DIR="${ROOT_DIR}/temp"
DIST_DIR="${ROOT_DIR}/dist"

IMAGE_NAME="mini-upload"
NAMESPACE="lovewy123"

VER=$1
if [ "$VER" == "" ]; then
  echo "version param must exists! like: 1.0.1"
  exit
fi
rm -rf ${TMP_DIR} ${DIST_DIR}
mkdir -p ${TMP_DIR}
mkdir -p ${DIST_DIR}
cd ${ROOT_DIR}
go mod tidy
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ${TMP_DIR}/linux_amd64/main
upx ${TMP_DIR}/linux_amd64/main
docker buildx build -t ${NAMESPACE}/${IMAGE_NAME}:${VER} .
docker save -o ${DIST_DIR}/${IMAGE_NAME}_${VER}.tar ${NAMESPACE}/${IMAGE_NAME}:${VER}
docker builder prune -f
rm -rf ${TMP_DIR}